<?php

/*
Plugin Name: Synergy Right Sidebar
Plugin Author: Maia Engeli
Description: This widgets switches the widget displayed in the right sidebar
*/


function widget_syn_rightsidebar() {

	global $wpdb;
	global $current_user;
	$table = $wpdb->prefix."master";
	$id = $wpdb->get_var("SELECT rightbar FROM $table WHERE uid=$current_user->ID");
	
	echo $before_widget; 
 	echo $before_title . $after_title;
	
	switch ($id) {
		case 1:
			widget_syn_search();
			break;
		case 2:
			widget_syn_message_system();
			break;
		case 3: 
			widget_syn_user_profile();
		break;
		case 4: 
			widget_syn_rules();
			break;
		case 5: 
			widget_syn_help();
		break;
		default:
			break;
	}
	
    echo $after_widget; 
}

register_sidebar_widget('Synergy RightSideBar', 'widget_syn_rightsidebar');

function syn_rightsidebar_install() {
	global $wpdb;
	$table = $wpdb->prefix."rightsidebar";
	$wpdb->query("DROP TABLE $table");
				 
	$schema = "CREATE TABLE $table (
        id INT(9) NOT NULL AUTO_INCREMENT,
        name VARCHAR(80),
        active TINYINT(1) DEFAULT 0,
		UNIQUE KEY id (id)
    );";
	$wpdb->query($schema);
	$wpdb->query("insert into $table (id, name) values (1, 'search'), (2, 'messages')," .
				 "(3, 'profile'), (4, 'rules'), (5, 'help')");
	$wpdb->query("UPDATE $table SET active = 1 WHERE id = 1");
}
add_action('activate_syn_rightsidebar/syn_rightsidebar.php', 'syn_rightsidebar_install');

?> 